package com.zzz.gp.service;

import com.zzz.gp.bean.Gp;
import com.zzz.gp.dao.GpMapper;
import com.zzz.gp.utils.HttpRequestUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @description: 查询实时数据，存在就入库
 * @author: ZZZ
 * @time: 2021/12/6 10:49
 */
@Slf4j
@Component
public class GpCodeService {
    @Autowired
    private GpMapper gpMapper;

    public void cleanCode() {
        gpMapper.cleanCode();
        log.info("=====清理表：get_code=====");
    }

    public void clean() {
//        gpMapper.deleteGpCode("%银行%");
//        gpMapper.deleteGpCode("%ST%");
//        gpMapper.deleteGpCode("%石油%");
//        gpMapper.deleteGpCode("%石化%");
//        gpMapper.deleteGpCode("%证券%");
    }

    @Async("myAsync")
    public void SZ1() {
        List<String> codeList = gpMapper.getCodeList();
        String sz = "https://hq.sinajs.cn/list=sz";
        //深交所主板代码包括000、001开头的两个系列六位编码。
        List<Gp> list = new ArrayList<>();
        for (int i = 1; i <= 999; i++) {
            String code = getCode(i);
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            if (codeList.contains(code)) {
                continue;
            }
            String result = HttpRequestUtil.httpRequest(sz + code);
            if (result.length() > 30) {
                int s = result.indexOf("\"") + 1;
                int e = result.indexOf(",");
                String name = result.substring(s, e);
                Gp gp = new Gp();
                gp.setCode(code);
                gp.setName(name);
                gp.setType("2");
                list.add(gp);
                //log.info(name + " " + code);

            }
        }
        if (list.size() > 0) {
            log.info("深交所(000):{}", list.size());
            gpMapper.batchInsert(list);
            clean();
        }

    }

    @Async("myAsync")
    public void SZ2(int start, int end) {
        List<String> codeList = gpMapper.getCodeList();
        String sz = "https://hq.sinajs.cn/list=sz";
        //深交所主板代码包括000、001开头的两个系列六位编码。
        List<Gp> list = new ArrayList<>();
        for (int i = start; i <= end; i++) {
            String code = "00" + i;
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            if (codeList.contains(code)) {
                continue;
            }
            String result = HttpRequestUtil.httpRequest(sz + code);
            if (result.length() > 30) {
                int s = result.indexOf("\"") + 1;
                int e = result.indexOf(",");
                String name = result.substring(s, e);
                Gp gp = new Gp();
                gp.setCode(code);
                gp.setName(name);
                gp.setType("2");
                list.add(gp);
                //log.info(name + " " + code);

            }
        }
        if (list.size() > 0) {
            log.info("深交所(00{}-00{}):{}", start, end, list.size());
            gpMapper.batchInsert(list);
            clean();
        }

    }

    @Async("myAsync")
    public void SZ3(int start, int end) {
        List<String> codeList = gpMapper.getCodeList();
        String sh = "https://hq.sinajs.cn/list=sz";
        String type = "2";
        List<Gp> list = new ArrayList<>();
        for (int i = start; i <= end; i++) {
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            if (codeList.contains("" + i)) {
                continue;
            }
            String result = HttpRequestUtil.httpRequest(sh + i);
            if (result.length() > 30) {
                int s = result.indexOf("\"") + 1;
                int e = result.indexOf(",");
                String name = result.substring(s, e);
                Gp gp = new Gp();
                gp.setCode("" + i);
                gp.setName(name);
                gp.setType(type);
                list.add(gp);
                //log.info(name + " " + i);

            }
        }
        if (list.size() > 0) {
            log.info("深交所({}-{}):{}", start, end, list.size());
            gpMapper.batchInsert(list);
            clean();
        }
    }

    @Async("myAsync")
    public void SH(int start, int end) {
        List<String> codeList = gpMapper.getCodeList();
        String sh = "https://hq.sinajs.cn/list=sh";
        String type = "1";
        List<Gp> list = new ArrayList<>();
        for (int i = start; i <= end; i++) {
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            if (codeList.contains("" + i)) {
                continue;
            }
            String result = HttpRequestUtil.httpRequest(sh + i);
            if (result.length() > 30) {
                int s = result.indexOf("\"") + 1;
                int e = result.indexOf(",");
                String name = result.substring(s, e);
                Gp gp = new Gp();
                gp.setCode("" + i);
                gp.setName(name);
                gp.setType(type);
                list.add(gp);
                //log.info(name + " " + i);

            }
        }
        if (list.size() > 0) {
            log.info("上交所({}-{}):{}", start, end, list.size());
            gpMapper.batchInsert(list);
            clean();
        }
    }

    public String getCode(int code) {
        String s = "" + code;
        if (s.length() == 1) {
            s = "00000" + s;
        } else if (s.length() == 2) {
            s = "0000" + s;
        } else {
            s = "000" + s;
        }
        return s;
    }

}
